﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NGUYENTHANHDONG.OOP.Inhertance___Polimophism.EXCERCISES.EXCERCISE_06
{
    public partial class Calculator : Form
    {
        public Calculator()
        {
            InitializeComponent();
        }

        private void Calculator_Load(object sender, EventArgs e)
        {
            cmbPhepTinh.Items.Add("Cộng");
            cmbPhepTinh.Items.Add("Trừ");
            cmbPhepTinh.Items.Add("Nhân");
            cmbPhepTinh.Items.Add("Chia");

        }

        private void btnXacNhan_Click(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            if (string.IsNullOrEmpty(txtSoa.Text))
            {
                errorProvider1.SetError(txtSoa, "Khong dc de trong !!");
                txtSoa.Focus();
            }
            else if(string.IsNullOrEmpty(txtSob.Text))
            {
                errorProvider1.SetError(txtSob, "Khong dc de trong !!");
                txtSob.Focus();
            }
            else if(string.IsNullOrEmpty(cmbPhepTinh.Text))
            {
                errorProvider1.SetError(cmbPhepTinh, "Ban chua chon phep tinh !!");

            }
            else
            {
                ClassThuaKe objthuake = new ClassThuaKe();
                if (cmbPhepTinh.Text == "Cộng")
                {
                    lsbKetQua.Items.Add (objthuake.Cong(float.Parse(txtSoa.Text), float.Parse(txtSob.Text)) + " ");
                }
                else if (cmbPhepTinh.Text == "Trừ")
                {
                    lsbKetQua.Items.Add(objthuake.Tru(float.Parse(txtSoa.Text), float.Parse(txtSob.Text)) + " ");
                }
                else if (cmbPhepTinh.Text == "Nhân")
                {
                    lsbKetQua.Items.Add(objthuake.Nhan(float.Parse(txtSoa.Text), float.Parse(txtSob.Text)) + " ");
                }
                else if (cmbPhepTinh.Text == "Chia")
                {
                    if(txtSoa.Text == "0" && txtSob.Text == "0")
                    {
                        errorProvider1.SetError(txtSob, "Result of function is underfined");
                        txtSob.Focus();
                    }
                    else if (txtSoa.Text != "0" && txtSob.Text == "0")
                    {
                        errorProvider1.SetError(txtSob, "Cannot divide by zero");
                        txtSob.Focus();
                    }
                    else
                    {
                        lsbKetQua.Items.Add(objthuake.Chia(float.Parse(txtSoa.Text), float.Parse(txtSob.Text)) + " ");
                    }
                }
            }
        }
    }
}
